package com.lb.jwitter.action;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Map; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;


 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocDuKienEditFormAction{
    private int id; 
    public void setId(int id) {
       this.id = id;
 }

    public int getId() {
       return id;
 }

    private TblQLDTTXALopMonHocDuKien tblQLDTTXALopMonHocDuKien; 
    public TblQLDTTXALopMonHocDuKien getTblQLDTTXALopMonHocDuKien() { 
        return tblQLDTTXALopMonHocDuKien; 
    } 
    private List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens; 
    public List<TblQLDTTXALopMonHocDuKien>  getTblQLDTTXALopMonHocDuKiens() {
         return tblQLDTTXALopMonHocDuKiens; 
      } 

    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    
    List<TblQLDTTXAHocVienDangKyMonHoc> tblQLDTTXAHocVienDangKyMonHocs;
    
    List<List<TblQLDTTXAHocVienDangKyMonHoc>> tblQLDTTXAHocVienDangKyMonHocTheoLops;
    
    List<TblQLDTTXALop> tblQLDTTXALops;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "them")){ 
        return "noprivilege"; 
    } 
        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 

        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 

        
        tblQLDTTXALopMonHocDuKien = tblQLDTTXALopMonHocDuKienService.gettblQLDTTXALopMonHocDuKien(new Long(id)); 

        if(tblQLDTTXALopMonHocDuKien != null){

            List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
            
        	ConditionReport con = new ConditionReport(); 
            con.setCol("monHocId.id");
            con.setValue1(tblQLDTTXALopMonHocDuKien.getMonHocId().getId());
            con.setOperator("eq");
            cons.add(con);	
            
        	ConditionReport con2 = new ConditionReport(); 
            con2.setCol("phieuDangKyId.hocKyId.id");
            con2.setValue1(tblQLDTTXALopMonHocDuKien.getHocKyId().getId());
            con2.setOperator("eq");
            cons.add(con2);	
            
            ConditionReport con3 = new ConditionReport(); 
            con3.setCol("daXepLop");
            con3.setValue1(false);
            con3.setOperator("eq");
            cons.add(con3);	
            
            ConditionReport con4 = new ConditionReport(); 
            con4.setCol("phieuDangKyId.hocVienId.ten");
            con4.setValue1("asc");
            con4.setOperator("order");
            cons.add(con4);	
            
            tblQLDTTXAHocVienDangKyMonHocs = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, 0, 0);
            
            tblQLDTTXAHocVienDangKyMonHocTheoLops = new ArrayList<List<TblQLDTTXAHocVienDangKyMonHoc>>();
            tblQLDTTXALops = new ArrayList<TblQLDTTXALop>();
            List<Long> lopIds = new ArrayList<Long>();
            
            for (TblQLDTTXAHocVienDangKyMonHoc tblQLDTTXAHocVienDangKyMonHoc : tblQLDTTXAHocVienDangKyMonHocs){
            	Long lopId = tblQLDTTXAHocVienDangKyMonHoc.getPhieuDangKyId().getHocVienId().getLopId().getId();
            	if (!lopIds.contains(lopId)){
            		lopIds.add(lopId);
            		tblQLDTTXALops.add(tblQLDTTXAHocVienDangKyMonHoc.getPhieuDangKyId().getHocVienId().getLopId());
            		tblQLDTTXAHocVienDangKyMonHocTheoLops.add(new ArrayList<TblQLDTTXAHocVienDangKyMonHoc>());
            	}
            	tblQLDTTXAHocVienDangKyMonHocTheoLops.get(lopIds.indexOf(lopId)).add(tblQLDTTXAHocVienDangKyMonHoc);
            }

//            tblQLDTTXAHocViens = new ArrayList<TblQLDTTXAHocVien> ();
//            for(TblQLDTTXAHocVienDangKyMonHoc tbl : tblQLDTTXAHocVienDangKyMonHocs){
//            	tblQLDTTXAHocViens.add(tbl.getPhieuDangKyId().getHocVienId());
//            }
        }
        

        
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_QLDTTXA_LopMonHoc_DuKien Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public List<TblQLDTTXAHocVien> getTblQLDTTXAHocViens() {
	return tblQLDTTXAHocViens;
}

public void setTblQLDTTXAHocViens(List<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
	this.tblQLDTTXAHocViens = tblQLDTTXAHocViens;
}

public List<TblQLDTTXAHocVienDangKyMonHoc> getTblQLDTTXAHocVienDangKyMonHocs() {
	return tblQLDTTXAHocVienDangKyMonHocs;
} 

public List<TblQLDTTXALop> getTblQLDTTXALops() {
	return tblQLDTTXALops;
} 

public List<List<TblQLDTTXAHocVienDangKyMonHoc>> getTblQLDTTXAHocVienDangKyMonHocTheoLops() {
	return tblQLDTTXAHocVienDangKyMonHocTheoLops;
} 
 } 
 
